package logic

import (
	"context"
	"errors"
	"fmt"
	"house/model/mysql"

	"house/house"
	"house/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseListPriceLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseListPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseListPriceLogic {
	return &HouseListPriceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseListPriceLogic) HouseListPrice(in *house.HouseListPriceRequest) (*house.HouseListPriceResponse, error) {
	// todo: add your logic here and delete this line

	var s []mysql.Shop
	var u mysql.User
	u.ID = uint(in.UserId)
	if err := u.Find(); err != nil {
		logx.Error("请前去登录")
		return nil, errors.New("请前去登录")
	}
	if in.PriceMin < 1000000 {
		logx.Error("最小值不能超过一百万")
		return nil, errors.New("最小值不能超过一百万")
	}
	if in.PriceMax > 3000000 {
		logx.Error("最大值不能超过三百万")
		return nil, errors.New("最大值不能超过三百万")
	}
	sn := mysql.DB.Where("price >= ? and price <= ?", in.PriceMin, in.PriceMax).Find(&s)
	fmt.Println(sn)
	var houerpricelist []*house.HouseListPrice
	for _, k := range s {
		houerpricelist = append(houerpricelist, &house.HouseListPrice{
			Title:  k.Title,
			Info:   k.Info,
			Price:  float32(k.Price),
			Num:    k.Num,
			Image:  k.Image,
			UserId: int64(k.UserId),
			Audit:  int64(k.Audit),
			Status: int64(k.Status),
		})

	}
	return &house.HouseListPriceResponse{
		List: houerpricelist,
	}, nil
}
